package com.example.library.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.library.common.Result;
import com.example.library.entity.Bookclass;
import com.example.library.service.IBookclassService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 胡绍杆
 * @since 2022-06-17
 */
@ResponseBody
@RestController
@RequestMapping("/bookclass")
public class BookclassController {
    @Resource
    private IBookclassService bookclassService;

    //新增和修改
    @PostMapping
    private Result save(@RequestBody Bookclass bookclass) {
        return Result.success(bookclassService.saveOrUpdate(bookclass));
    }

    //查询所有数据
    @GetMapping
    public Result index(){
        return Result.success(bookclassService.list());
    }

    //分页和搜索
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam String className) {
        IPage<Bookclass> page=new Page<>(pageNum,pageSize);
        QueryWrapper<Bookclass> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("class_name",className);
        return  Result.success(bookclassService.page(page,queryWrapper));
    }

    //按classId查找图书类别
    @GetMapping("/classId")
    public Result findPage(@RequestParam Integer classId) {
        QueryWrapper<Bookclass> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("class_id",classId);
        return  Result.success(bookclassService.list(queryWrapper));
    }

    /**
     * yang
     * 返回所有图书类别
     */
    @GetMapping("/getBookClassY")
    public Result getBookClassY(){
        return Result.success(bookclassService.list());
    }
}
